//
//  RegisterViewController.m
//  WonderfulNotes
//
//  Created by 千锋 on 16/1/12.
//  Copyright © 2016年 andy. All rights reserved.
//

#import "RegisterViewController.h"
#import "loginButton.h"
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *emailTF;
@property (weak, nonatomic) IBOutlet loginButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    [self checkTF];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)userNameEditingChanged:(UITextField *)sender {
    
    [self checkTF];
}
- (IBAction)passwordTFEditingChanged:(UITextField *)sender {
    
    [self checkTF];
}

-(void)checkTF{
    
    if (self.userNameTF.text.length>0 && self.passwordTF.text.length>0) {
    
        self.registerBtn.enabled = YES;
    }else{
        
        self.registerBtn.enabled = NO;
    }
    
    
    
}

- (IBAction)registerBtnPressed:(loginButton *)sender {
    
    BmobUser * user = [[BmobUser alloc]init];
    user.username = self.userNameTF.text;
    NSString * pwdMD5Str = [EncryptionTools MD5StringFromString:self.passwordTF.text];
    user.password = pwdMD5Str;
    if (self.emailTF.text.length>0) {
        user.email = self.emailTF.text;
    }
    [user signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //[self.navigationController popViewControllerAnimated:YES];
                UIStoryboard * MainSb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                
                [self presentViewController:[MainSb instantiateViewControllerWithIdentifier:@"LoginViewController"] animated:YES completion:nil];
            });
        }else{
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
        }
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
